﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Thallo.Test.Mock {

    [ServiceContract]
    //[ServiceCachedable]
    public interface IBankAccount {

        [OperationContract]
        decimal GetAccount(string account);

        AccountInfo FetchAccountInfo(Condition condition);

        [OperationContract]
        [Cachedable(MethodHandlerType = typeof(MockMethodHandler), DependencyGroup="g1")]
        DateTime GetDate();

        [OperationContract]
        void UpdateAccount(string account, decimal value);
        
        [OperationContract]
        void DeleteAccount(string account);
    }

    [ServiceContract]
    public interface IStore {
        [OperationContract]
        string GetCurrent();

        [OperationContract]
        void Update();
    }


    [ServiceContract]
    public interface IStore2 {
        [OperationContract]
        string GetCurrent();

        [OperationContract]
        void Update();
    }

    public class BankAccount : IBankAccount {

        public static decimal amount = 12m;
        public decimal GetAccount(string account) {
            Console.WriteLine("get account");
            return amount;
        }

        
        public DateTime GetDate() {
            return DateTime.Now;
        }

        public void UpdateAccount(string account, decimal value) {
            Console.WriteLine("update account");
            amount += value;
        }


        public void DeleteAccount(string account) {
            Console.WriteLine("remove account");
        }

        public AccountInfo FetchAccountInfo(Condition condition)
        {
            Console.WriteLine("fetch account");

            return new AccountInfo() { Id = condition.Id, Name = condition.Name, CreateDate = DateTime.Now };
        }
    }


    public class Condition
    {
        public int Id { get; set; }

        public string Name { get; set; }
    }

    public class AccountInfo {
        public int Id { get; set; }

        public string Name { get; set; }

        public decimal Account { get; set; }

        public DateTime CreateDate { get; set; }
    }
    
    class ConditionKeyParser:IParameterParser {
        public string ParseKey(object val) {
            var c = val as Condition;
            return c.Name + "_" + c.Id;
        }


        public Type ValueType {
            get;
            set;
        }

        public string Prefix {
            get;
            set;
        }

        public IParameterParser Clone() {
            return this.MemberwiseClone() as IParameterParser;
        }
    }

    public class MockMethodHandler:IMethodHandler {
        public static bool BeforeInvoked;
        public static bool AfterInvoked;

        public bool BeforeInvoke(System.Reflection.MethodBase mi, System.Collections.IEnumerable inputs) {
            BeforeInvoked = true;
            return false;
        }

        public bool AfterInvoke(System.Reflection.MethodBase mi, System.Collections.IEnumerable inputs) {
            AfterInvoked = true;
            return false;   
        }
    }


    public class Store : IStore {
        DateTime _time = DateTime.Now;

        public string GetCurrent() {
            return _time.ToLongTimeString();
        }

        public void Update() {
            _time = DateTime.Now;
        }
    }


}
